package com.sndscl.rpc.spring;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

/**
 * @Description
 * @Author lsh
 * @Date 2020/10/23 15:06
 **/
@Component
public class SpringApplicationContextUtils implements ApplicationContextAware {

    private static ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context = applicationContext;
    }

    public static <T> T getBean(Class<T> aClass) {
        if (context != null) {
            return context.getBean(aClass);
        }
        return null;
    }

    public static Object getBean(String beanName){
        if (context != null) {
            return context.getBean(beanName);
        }
        return null;
    }

    public static Environment getEnvironment(){
        return context.getEnvironment();
    }
}
